from flask import Blueprint, jsonify
from app.models.task import Task
from app.utils.response import APIResponse
from app.utils.errors import NotFoundError
from app.utils.pagination import paginate

bp = Blueprint('task', __name__)

@bp.route('/tasks', methods=['GET'])
def list_tasks():
    """获取任务列表"""
    page = request.args.get('page', 1, type=int)
    per_page = request.args.get('per_page', 20, type=int)
    
    query = Task.query.order_by(Task.created_at.desc())
    pagination = paginate(query, page, per_page)
    
    return APIResponse.success(
        data=pagination.to_dict(),
        message="Tasks retrieved successfully"
    )

@bp.route('/tasks/<string:task_id>', methods=['GET'])
def get_task(task_id):
    """获取任务详情"""
    task = Task.query.filter_by(celery_id=task_id).first()
    if not task:
        raise NotFoundError(f"Task {task_id} not found")
    
    return APIResponse.success(
        data=task.to_dict(),
        message="Task retrieved successfully"
    ) 